package adminapilogic

import (
	v1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/admin/v1"
	"code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/common"
	"code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/common/enums"
	"code.bydev.io/cht/customer/kyc_admin_service/internal/app/serv"
	"context"
	pb "git.bybit.com/svc/stub/pkg/pb/api/kycadmin"

	"code.bydev.io/cht/customer/kyc_admin_service/service/private/internal/svc"
)

type AdminAPIGetKycActionDetailLogic struct {
	svcCtx *svc.ServiceContext
}

func NewAdminAPIGetKycActionDetailLogic(
	svcCtx *svc.ServiceContext,
) *AdminAPIGetKycActionDetailLogic {
	return &AdminAPIGetKycActionDetailLogic{
		svcCtx: svcCtx,
	}
}

// KYC 认证查询详情
func (l *AdminAPIGetKycActionDetailLogic) GetKycActionDetail(
	ctx context.Context,
	in *v1.GetKycActionDetailRequest,
) (*v1.KycActionDetailResponse, error) {
	oldResp, err := serv.NewService(l.svcCtx.NothingEnv).GetKycActionDetail(ctx, &pb.GetKycActionDetailRequest{
		Id:       in.GetId(),
		BrokerId: in.GetBrokerId(),
	})
	if err != nil {
		return nil, err
	}
	var respErr *common.Error
	if oldResp.GetError() != nil {
		respErr = &common.Error{
			Code:    oldResp.GetError().GetErrorCode(),
			Message: oldResp.GetError().GetInternalDebugMessage(),
		}
	}
	var images *v1.KycActionDetailImagesItem
	if oldResp.GetImages() != nil {
		images = &v1.KycActionDetailImagesItem{
			DocFront:  oldResp.GetImages().GetDocFront(),
			DocBack:   oldResp.GetImages().GetDocBack(),
			FaceImage: oldResp.GetImages().GetFaceImage(),
		}
	}
	var faceMatch *v1.KycActionDetailFaceMatchItem
	if oldResp.GetFaceMatch() != nil {
		faceMatch = &v1.KycActionDetailFaceMatchItem{
			Similarity: oldResp.GetFaceMatch().GetSimilarity(),
			Validity:   oldResp.GetFaceMatch().GetValidity(),
		}
	}
	var bizDecision *v1.KycActionDetailBizDecisionItem
	if oldResp.GetBizDecision() != nil {
		bizDecision = &v1.KycActionDetailBizDecisionItem{
			MemberId: oldResp.GetBizDecision().GetMemberId(),
			Decision: oldResp.GetBizDecision().GetDecision(),
		}
	}
	var userData *v1.KycActionDetailUserItem
	if oldResp.GetUserData() != nil {
		userData = &v1.KycActionDetailUserItem{
			FirstName:        oldResp.GetUserData().GetFirstName(),
			LastName:         oldResp.GetUserData().GetLastName(),
			Bob:              oldResp.GetUserData().GetBob(),
			City:             oldResp.GetUserData().GetCity(),
			Nationality:      oldResp.GetUserData().GetNationality(),
			PostalCode:       oldResp.GetUserData().GetPostalCode(),
			FormattedAddress: oldResp.GetUserData().GetFormattedAddress(),
		}
	}
	var cardData *v1.KycActionDetailCardItem
	if oldResp.GetCardData() != nil {
		cardData = &v1.KycActionDetailCardItem{
			DocType:        oldResp.GetCardData().GetDocType(),
			DocSubtype:     oldResp.GetCardData().GetDocSubtype(),
			Country:        oldResp.GetCardData().GetCountry(),
			ExpireDate:     oldResp.GetCardData().GetExpireDate(),
			IssuingDate:    oldResp.GetCardData().GetIssuingDate(),
			DocNumber:      oldResp.GetCardData().GetDocNumber(),
			PersonalNumber: oldResp.GetCardData().GetPersonalNumber(),
		}
	}
	var eventData *v1.KycActionDetailEventItem
	if oldResp.GetEventData() != nil {
		eventData = &v1.KycActionDetailEventItem{
			ExternalUid:      oldResp.GetEventData().GetExternalUid(),
			RecordId:         oldResp.GetEventData().GetRecordId(),
			Source:           oldResp.GetEventData().GetSource(),
			ProviderCreateAt: oldResp.GetEventData().GetProviderCreateAt(),
			ClientIp:         oldResp.GetEventData().GetClientIp(),
			From:             oldResp.GetEventData().GetFrom(),
		}
	}
	return &v1.KycActionDetailResponse{
		Error:            respErr,
		Id:               oldResp.GetId(),
		ProviderDecision: oldResp.GetProviderDecision(),
		Images:           images,
		FaceMatch:        faceMatch,
		BizDecision:      bizDecision,
		UserData:         userData,
		CardData:         cardData,
		EventData:        eventData,
		DecisionProcess:  enums.DecisionProcess(oldResp.GetDecisionProcess()),
		IsTransfer:       oldResp.GetIsTransfer(),
		TransChain:       oldResp.GetTransChain(),
	}, nil
}
